package com.hljx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hljx.config.Result;
import com.hljx.dto.SysUserDTO;
import com.hljx.dto.SysUserPageDTO;
import com.hljx.entity.SysAccountingUnit;
import com.hljx.entity.SysUser;
import com.hljx.service.SysAccountingUnitService;
import com.hljx.service.SysRoleService;
import com.hljx.service.SysUserService;
import com.hljx.vo.RoleVO;
import com.hljx.vo.UserLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author gx
 * @since 2023-09-25
 */
@RestController
@RequestMapping("/sysUser")
@Tag(name = "用户管理")
public class SysUserController {
    
    @Autowired
    SysUserService userService;

    @Autowired
    SysRoleService roleService;

    @Autowired
    SysAccountingUnitService accountingUnitService;

    @Operation(summary = "登陆")
    @GetMapping("/login")
    public Result<UserLoginVO> login(@RequestParam("code") String code, @RequestParam("passWord") String passWord) {
        return Result.success(userService.login(code,passWord));
    }

    @Operation(summary = "角色下拉")
    @GetMapping(value = "/getRoles")
    public Result<List<RoleVO>> getRoles() {
        return Result.success(roleService.getRoles());
    }

    @Operation(summary = "核算单元下拉")
    @GetMapping(value = "/listAllAccountingUnit")
    public Result<List<SysAccountingUnit>> listAllAccountingUnit() {
        return Result.success(accountingUnitService.listAllAccountingUnit());
    }

    @Operation(summary = "分页查询所有用户")
    @PostMapping(value = "/listSysUser")
    public Result<IPage<SysUser>> listSysUser(@RequestBody SysUserPageDTO sysUserPageDTO) {
        return Result.success(userService.listSysUser(sysUserPageDTO));
    }

    @Operation(summary = "添加或修改用户")
    @PostMapping(value = "/saveOrUpdateNew")
    public Result<Boolean> saveOrUpdateNew(@RequestBody SysUserDTO sysUserDTO) {
        return Result.success(userService.saveOrUpdateNew(sysUserDTO));
    }

    @Operation(summary = "删除用户")
    @GetMapping("/removeById")
    public Result<Boolean> removeById(@RequestParam("userId") Long userId) {
        return Result.success(userService.removeByIdNew(userId));
    }

    @Operation(summary = "修改密码")
    @GetMapping("/resetPassword")
    public Result<Boolean> resetPassword(@RequestParam("userId") Long userId,@RequestParam("password") String password) {
        return Result.success(userService.resetPassword(userId,password));
    }

    @Operation(summary = "修改用户启用状态:0禁止1启用")
    @GetMapping("/updateDictItemStatus")
    public Result<Boolean> updateDictItemStatus(@RequestParam("userId") Long userId,
                                                      @RequestParam("status") Integer status) {
        return Result.success(userService.updateUserStatus(userId,status));
    }
}
